package com.eascs.admin.o2o.settle.dao;

import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.o2o.settle.entity.O2oSettleEntity;
import com.eascs.admin.o2o.settle.vo.O2oSettleVo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;

@Repository
public class O2oSettleDao extends BaseO2oDao {

	public PageResult<Object[]> getSettlePage(O2oSettleVo vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getPageIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append(" SELECT e.loan_data_id,e.bill_code,e.create_time,e.loan_user_name,CONCAT(y.ltd_code,'-',y.ltd_name) AS ltd_name ");
		querySql.append(" ,e.setlle_time,e.dc_total,e.dc_load_total,e.flow_status FROM ");
		querySql.append(" o2o_ea_loan_settle e LEFT JOIN o2o_loanapply y on y.bill_code=e.bill_code where /**DATA_PERMISSION(y.ltd)**/ is_delete='N' ");
		if (!StringUtil.isEmpty(vo.getLoanUserName())) {
			querySql.append(" and e.loan_user_name like ? ");
			sqlPageQuery.addLikeParam(vo.getLoanUserName());
		}
		if (!StringUtil.isEmpty(vo.getBillCode())) {
			querySql.append(" and e.bill_code like ? ");
			sqlPageQuery.addLikeParam(vo.getBillCode());
		}
		if (!StringUtil.isEmpty(vo.getcStartTime())) {
			querySql.append(" and e.create_time >= ? ");
			sqlPageQuery.addParam(vo.getcStartTime());
		}
		if (!StringUtil.isEmpty(vo.getcEndTime())) {
			querySql.append(" and e.create_time <= ? ");
			sqlPageQuery.addParam(vo.getcEndTime());
		}
		if (!StringUtil.isEmpty(vo.getDcStartTime())) {
			querySql.append(" and e.setlle_time >= ? ");
			sqlPageQuery.addParam(vo.getDcStartTime());
		}
		if (!StringUtil.isEmpty(vo.getDcEndTime())) {
			querySql.append(" and e.setlle_time <= ? ");
			sqlPageQuery.addLikeParam(vo.getDcEndTime());
		}
		querySql.append(" ORDER BY e.create_time DESC ");
		sqlPageQuery.setQuerySql(querySql.toString());
		return this.findDataWithPage(sqlPageQuery);
	}

	public O2oSettleEntity findEntity(O2oSettleVo vo) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from o2o_ea_loan_settle where 1=1 ");
		StringBuffer params = new StringBuffer();
		if (!StringUtil.isEmpty(vo.getId())) {
			params.append(vo.getId());
			sql.append(" and id = ? ");
		}
		if (!StringUtil.isEmpty(vo.getEaLoadId())) {
			if (params.length() > 0)
				params.append(",").append(vo.getEaLoadId());
			else
				params.append(vo.getEaLoadId());
			sql.append(" and loan_data_id = ? ");
		}
		if (StringUtil.isEmpty(vo.getId()) && StringUtil.isEmpty(vo.getEaLoadId())) {
			return null;
		}
		return this.findEntity(sql.toString(), O2oSettleEntity.class, params);
	}
}
